﻿//------------------------------------------------------------------------------
// The contents of this file are subject to the Microsoft Public License (Ms-PL)
// You may obtain a copy of the License at http://psadmin.codeplex.com/license. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, 
// WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations 
// under the License.
// 
// The initial developer is Ben Foster (ben.foster@retroviz.com)
// Copyright (c) Retroviz Limited (http://www.retroviz.com).
// All Rights Reserved.
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSAdmin.Core.Security;
using PSAdmin.Core.Interfaces;
using NHibernate;
using NHibernate.Linq;
using PSAdmin.Core;
using NHibernate.Criterion;

namespace PSAdmin.Data.Repositories
{
    public class UserRoleRepository : IUserRoleRepository
    {
        private ISession _session;
        
        public UserRoleRepository(ISession session) {
            _session = session;
        }

        public Guid Save(UserRole entity) {
            using (var txn = _session.BeginTransaction()) {
                _session.SaveOrUpdate(entity);
                txn.Commit();
            }
            return entity.Id;
        }

        public void Delete(UserRole entity) {
            using (var txn = _session.BeginTransaction()) {
                _session.Delete(entity);
                txn.Commit();
            }
        }

        public UserRole GetById(Guid id) {
            return _session.Get<UserRole>(id);
        }

        public UserRole GetByName(string roleName) {
            return _session.Linq<UserRole>()
                .FirstOrDefault(x => x.Name.Equals(roleName, StringComparison.InvariantCultureIgnoreCase));
        }

        public ICollection<UserRole> GetAll() {
            return _session.CreateCriteria<UserRole>()
                .List<UserRole>();
        }

        public PagedList<UserRole> GetPaged(int pageIndex, int pageSize) {
            var rowCount = _session.CreateCriteria<UserRole>()
                                .SetProjection(Projections.RowCount())
                                .FutureValue<Int32>();

            var results = _session.CreateCriteria<UserRole>()
                .SetFirstResult((pageIndex - 1) * pageSize)
                .SetMaxResults(pageSize)
                .Future<UserRole>()
                .ToList<UserRole>();

            return new PagedList<UserRole>(results, pageSize, pageSize, rowCount.Value);
        }

        public ICollection<UserRole> GetUserRoles(string username) {
            var userRoles =
                _session.CreateCriteria<UserRole>()
                    .CreateCriteria("Users")
                        .Add(Expression.Eq("Username", username.ToLower()))
                .List<UserRole>();

            return userRoles;
        }
    }
}
